<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Model_ProductPrice extends Zend_Db_Table{
	
	protected $_name    = "product_price";
    protected $_primary = "id";
    
    public function saveData($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $customer_id = $data['customer_id'];
            unset($data['customer_id']);
            
            foreach($data as $product){
                $id = $product['id'];
                
                $row['product_id'] = $id;
                $row['customer_id'] = $customer_id;
                $row['discount_type'] = $product['discount_type'];
                $row['discount_value'] = $product['discount'];
                $row['price'] = $product['price'];
                
                if($this->checkExists($id, $customer_id)){
                    $db->update("product_price", $row, "product_id = $id AND customer_id = $customer_id");
                }else{
                    $db->insert('product_price', $row);
                }
            }
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function getDatas($customer_id){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
        
        $cols = array('product_id', 'customer_id', 'price', 'discount_type', 'discount_value');
        
        if($this->checkCustomerExists($customer_id)){
            $select = $db->select()
                    ->from(array('p' => 'product_buys'), array('id', 'name', 'net', 'price AS p_price'))
                    //->joinRight(array('p' => 'product_buys'), "p.id = pp.product_id", array('id', 'name', 'net', 'price AS p_price'))
                    ->joinLeft(array('pp' => 'product_price'), "pp.customer_id = $customer_id AND pp.product_id = p.id", $cols)
                    ->joinLeft(array('b' => 'brands'), "p.brand = b.id", array("brand"))
                    ->order('p.brand')
                    ->order('p.name')
                    ->order('p.id')
                    ->where('p.showinsale = 1')
                    ->where('p.customer = ?', $customer_id)
                    ->orWhere('p.customer = 0');
        }else{
            $select = $db->select()
                    ->from(array('p' => 'product_buys'), array('id', 'name', 'net', 'price_sell AS p_price'))
                    ->join(array('b' => 'brands'), "p.brand = b.id", array("brand"))
                    ->order('p.id')
                    ->order('p.brand')
                    ->order("p.net")
                    ->where('p.customer = ?', $customer_id)
                    ->orWhere('p.customer = 0');
        }
        
        //echo $select;    
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function updatePrice($data){
        $db = Zend_Registry::get('db');
        
        $product_id = $data['product_id'];
        $customer_id = $data['customer_id'];
        
        $db->beginTransaction();
        
        try{
            if($this->checkExists($data['product_id'], $data['customer_id'])){
                $db->update("product_price", $data, "customer_id = $customer_id AND product_id = $product_id");
            }else{
                
                $db->insert("product_price", $data);
            }
            
            $db->commit();
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function checkExists($product_id, $customer_id){
        $db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
        
        $select = $db->select()
                    ->from(array('p' => 'product_price'), array('product_id', 'customer_id'))
                    ->where('p.product_id = ?', $product_id, 'INTEGER')
                    ->where('p.customer_id = ?', $customer_id, 'INTEGER');
        
        $rs = $db->fetchRow($select);
        
        if($rs){
            return true;
        }
        
        return false;
    }
    
    public function checkCustomerExists($id){
        $option = array('table' => 'product_price', 'field' => 'customer_id');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($id)){
            return true;
        }
        
        return false;
    }
    
    public function checkDiscountType($customer_id){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
        
        $select = $db->select()
                    ->from(array('c' => 'customers'), array('discount_type'))
                    ->where('c.id = ?', $customer_id, 'INTEGER');
        
        $rs = $db->fetchRow($select);
        
        if($rs['discount_type']){
            return true;
        }
        
        return false;
    }
}